//
//  TKIMConverseCell.m
//  TKIM
//
//  Created by liupm on 15/10/30.
//  Copyright © 2015年 liupm. All rights reserved.
//

#import "TKIMConverseCell.h"

#define kTimeLabelWidth 110
#define kTimeLabelRightPadding 5
#define kRedDotWidth 25.0

@implementation TKIMConverseCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        [self drawView];
    }
    return self;
}


/**
 *  @Author lpm, 15-10-29 10:10:28
 *
 *  @brief  组装界面
 */
-(void)drawView
{
    [self addSubview:self.headView];
    [self addSubview:self.time];
    [self addSubview:self.titleLabel];
    [self addSubview:self.subLabel];
    [self addSubview:self.redDot];
}





-(UIImageView *)headView
{
    if (!_headView)
    {
        _headView =  [[UIImageView alloc] initWithFrame:CGRectMake(10, 15.0/2, 45, 45)];
        _headView.aliCornerRadius = 45/2.0;
    }
    return _headView;
}

-(UIButton *)redDot
{
    if (!_redDot)
    {
        _redDot = [[UIButton alloc] initWithFrame:CGRectMake(self.subLabel.right,self.subLabel.top, kRedDotWidth, 18)];
        _redDot.backgroundColor = [TKIMColorHelper stringToColor:@"#ff0000"];
        _redDot.layer.cornerRadius = 9.0f;
        _redDot.titleLabel.font = [UIFont systemFontOfSize:12.0f];
        _redDot.titleLabel.textColor = [UIColor whiteColor];
        _redDot.hidden = YES;
    }
    return _redDot;
}



/**
 *  @Author lpm, 15-11-06 17:11:26
 *
 *  @brief  设置消息数
 *
 *  @param msgCount
 */
-(void)setMsgCount:(NSInteger)msgCount
{
    if(msgCount == 0)
    {
        if (!self.redDot.isHidden)
        {
            self.redDot.hidden = YES;
        }
    }
    else
    {
        if (self.redDot.isHidden)
        {
            self.redDot.hidden = NO;
        }
        
        if (msgCount<10) {
            
              _redDot.frame = CGRectMake(self.subLabel.right,self.subLabel.top, 20.0, 20.0);
                _redDot.layer.cornerRadius = 10.0f;
        }else{
            _redDot.frame = CGRectMake(self.subLabel.right,self.subLabel.top, 25.0, 18.0);
            _redDot.layer.cornerRadius = 9.0f;
        }
        NSString *str =[NSString stringWithFormat:@"%ld",(long)msgCount];
        if (msgCount>99) {
            str=@"99+";
        }
        
        [_redDot setTitle:str forState:UIControlStateNormal];
    }
}


-(UILabel *)titleLabel
{
    if (!_titleLabel)
    {
        CGFloat x = self.headView.frame.size.width+self.headView.frame.origin.x+10;
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, self.headView.frame.origin.y+2, TKIMScreenWidth-x-kTimeLabelWidth-kTimeLabelRightPadding, 20)];
        _titleLabel.font = [UIFont systemFontOfSize:17.0f];
        _titleLabel.textColor = [UIColor grayColor];
    }
    return _titleLabel;
}



-(UILabel *)subLabel
{
    if (!_subLabel)
    {
        CGFloat width = TKIMScreenWidth - kRedDotWidth - kTimeLabelRightPadding - self.titleLabel.frame.origin.x;
        
//        _subLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.bottom+3, self.titleLabel.frame.size.width+30,20)];
        _subLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.bottom+3, width,20)];
        _subLabel.font = [UIFont systemFontOfSize:14.0f];
        _subLabel.textColor = [UIColor lightGrayColor];
    }
    return _subLabel;
}




-(UILabel *)time
{
    if (!_time)
    {
//        _time = [[UILabel alloc] initWithFrame:CGRectMake(self.titleLabel.right,self.titleLabel.top, 70, self.titleLabel.height)];
        _time = [[UILabel alloc]initWithFrame:CGRectMake(TKIMScreenWidth - kTimeLabelWidth-kTimeLabelRightPadding, self.headView.frame.origin.y+2, kTimeLabelWidth, 20)];
        _time.font = [UIFont systemFontOfSize:12.0f];
        _time.textColor = [UIColor lightGrayColor];
        _time.textAlignment = NSTextAlignmentRight;
        
    }
    return _time;
}


@end
